//
//  TPWeibo.swift
//  sonic
//
//  Created by 魔码传媒 on 17/08/2017.
//  Copyright © 2017 magicode. All rights reserved.
//

import UIKit

class TPWeibo: NSObject, WeiboSDKDelegate
{
    func setup()
    {
        WeiboSDK.enableDebugMode(false)
        WeiboSDK.registerApp(SRef.Const(const: .weibo_key))
    }
    
    static func check() -> Bool
    {
        return WeiboSDK.isWeiboAppInstalled()
    }
    
    func handUrl(url: URL) -> Bool
    {
        return WeiboSDK.handleOpen(url, delegate: self)
    }
    
    static func login()
    {
        let request : WBAuthorizeRequest = WBAuthorizeRequest.request() as! WBAuthorizeRequest
        request.redirectURI = SRef.Const(const: .weibo_redirect_uri)
        request.scope = "all"
        request.userInfo = ["SSO_From":"SendMessageToWeiboViewController",
        "Other_Info_1" : NSNumber.init(integerLiteral: 123),
        "Other_Info_2" : ["obj1" , "obj2"],
        "Other_Info_3" : ["key1" : "obj1" , "key2" : "obj2"]]
        WeiboSDK.send(request)
    }
    
    /**
     收到一个来自微博客户端程序的请求
     
     收到微博的请求后，第三方应用应该按照请求类型进行处理，处理完后必须通过 [WeiboSDK sendResponse:] 将结果回传给微博
     @param request 具体的请求对象
     */
    func didReceiveWeiboRequest(_ request:WBBaseRequest)
    {
    
    }

    /**
     收到一个来自微博客户端程序的响应
     
     收到微博的响应后，第三方应用可以通过响应类型、响应的数据和 WBBaseResponse.userInfo 中的数据完成自己的功能
     @param response 具体的响应对象
     */
    func didReceiveWeiboResponse(_ response:WBBaseResponse)
    {
        if response.isKind(of:WBAuthorizeResponse.self)
        {
            let resp = response as! WBAuthorizeResponse
        
            if resp.statusCode == .success
            {
                let openId = resp.userID
                let token = resp.accessToken
                
                _ = WBHttpRequest.init(accessToken: token, url: "https://api.weibo.com/2/users/show.json", httpMethod: "GET", params: ["uid": openId!], delegate: self as! WBHttpRequestDelegate, withTag: "hello_xixi")
                
                var userInfo = response.userInfo as Dictionary
//                NotificationCenter.defaultCenter.postNotificationName("SINA_CODE", object: nil, userInfo: userInfo)
                
//                WBHttpRequest.request(withAccessToken: token, "https://api.weibo.com/2/users/show.json", "GET", ["uid": openId], self, "hello_xixi")
//                [WBHttpRequest requestWithAccessToken:toke url:@"https://api.weibo.com/2/users/show.json" httpMethod:@"GET" params:[NSDictionary  dictionaryWithObject:openId forKey:@"uid"] delegate:(id)self withTag:@"hello_xixi"];
            }
        }
    }
    
    func request(request:WBHttpRequest, data:Data)
    {
        //转换数据格式
        let content = try! JSONSerialization.jsonObject(with: data,
                                                    options: JSONSerialization.ReadingOptions.mutableContainers) as! NSMutableDictionary
        
        print(content)
        
//        NSDictionary *content = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];/
    //    NSLog(@"%@ i am result",content); //这里会返回 一些Base Info
    
    //    SeWeiboEntity *wei = [SeWeiboEntity mj_objectArrayWithKeyValuesArray:content];
    
//        let wei = SeWeiboEntity.init(content)
    
    //    if([MGCLoginRegisterViewController genericLoginProcess:wei])
    //    {
    //        //UIApplication.sharedApplication.delegate.window.rootViewController = [[MGCTabBarController alloc] init];
    //    }
    }
    

    
    
}

